<template>
  <div class="artile-detail" :class="{ 'is-mobile': $isMobile }">
    <div>
      <div class="title">{{ title }}</div>
      <div class="classify">
        <div>信息来源：{{ classify || "未知" }}</div>
        <div class="publishTime">发布时间： {{ formatTime(publishTime) }}</div>
      </div>
    </div>
    <div class="detail" v-html="detail"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      classify: "",
      publishTime: "",
      detail: "",
      title: "",
    };
  },

  mounted() {
    this.fetchData();

    this.formatArticle()
  },

  methods: {
    async fetchData() {
      const loading = this.$loading();
      const { id } = this.$route.params;
      const { data } = await this.$fetch.get(`/api/article/${id}`);
      loading.close();
      this.classify = data.classify;
      this.publishTime = data.publishTime;
      this.detail = data.detail;
      this.title = data.title;
    },
    formatTime(time) {
      if (!time) {
        return "暂无";
      }
      const date = new Date(time);
      const year = date.getFullYear();
      const month = `0${date.getMonth() + 1}`.slice(-2); // 补零操作
      const day = `0${date.getDate()}`.slice(-2); // 补零操作

      return `${year}-${month}-${day}`;
    },

    formatArticle() {
      if (this.$isMobile) {
        setTimeout(() => {
          this.$nextTick(() => {
            document.querySelectorAll(".artile-detail.is-mobile img").forEach(img => {
              console.log("img", img)
              img.style.width = "100%";
              img.style.height = "auto"

            })
          })
        }, 500);
      }

    }
  },
};
</script>

<style lang="less" scoped>
.artile-detail {
  background: #fff;
  padding: 30px 22px;
  font-family: PingFangSC, PingFang SC;


  .title {
    font-weight: 400;
    font-size: 36px;
    color: #333333;
    line-height: 50px;
    text-align: left;
    font-style: normal;
    word-break: break-all;
  }

  .classify {
    margin-top: 29px;
    border-bottom: 1px solid #efefef;
    padding-bottom: 14px;
    font-weight: 400;
    font-size: 14px;
    color: #aeaeae;
    line-height: 20px;
    text-align: left;
    font-style: normal;
    display: flex;
    flex-direction: row;


    .publishTime {
      margin-left: 10px;
    }
  }

  .detail {
    margin-top: 34px;
    color: #333333;
    font-weight: 400;
    font-size: 18px;
    color: #333333;
    line-height: 35px;
    text-align: left;
    font-style: normal;
  }
}

.artile-detail.is-mobile {
  padding: 10px;
  margin-bottom: 10px;


  .title {
    font-size: 24px;
    line-height: 30px;
  }

  .classify {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    margin-top: 5px;
    padding-bottom: 5px;


    .publishTime {
      margin-left: 0
    }
  }

  .detail {
    margin-top: 10px;
    line-height: 24px;

    img {
      width: 100% !important;
    }
  }
}
</style>
